package com.mahiru.mahiruaiagent.rag;

import org.springframework.ai.chat.client.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.filter.Filter;
import org.springframework.ai.vectorstore.filter.FilterExpressionBuilder;

/**
 * 创建自定义rag检索增强顾问工厂
 *
 * @Author Enkidu
 * @Date 2025/7/7 16:32
 */
public class ChatAppRagCustomAdvisorFactory {

    public static Advisor createChatAppRagCustomAdvisor(VectorStore vectorStore, String status) {
        // 创建过滤表达式
        Filter.Expression expression = new FilterExpressionBuilder().eq("learning_status", status).build();
        // 创建向量检索器
        VectorStoreDocumentRetriever documentRetriever = VectorStoreDocumentRetriever.builder()
                .vectorStore(vectorStore)
                .filterExpression(expression)
                .similarityThreshold(0.5)
                .topK(3)
                .build();
        // 创建检索增强顾问
        return RetrievalAugmentationAdvisor.builder()
                .documentRetriever(documentRetriever)
                .queryAugmenter(ChatAppContextualQueryAugmenterFactory.createInstance())
                .build();
    }
}
